<?php
namespace app\admin\controller;

use app\admin\logic\LogLogic;
use my\Security;
use think\Db;
use think\Env;

class PayLog extends Base
{
    protected $map = array('a.order_type'=>array('in',array(1,2)),'order_status'=>array('in',array('45','50')));//结算日志的订单状态应为已付款之后的

    public function _initialize()
    {
        parent::_initialize();
        $base_condition = $this->base_condition;
        if($base_condition)$this->map['start_code'] = $base_condition['region_code'];
    }

    public function index(){
        $config['url'] = url('pay_log/table');
        //$config['local'] = Env::get('coordinate'.ENV.'_payLog');
        $config['local'] = '/admin/PayLog/local';
        $this->assign($config);
        return view();
    }

    public function table()
    {
        $page = input('get.page');
        $limit = input('get.limit');
        $logic = new LogLogic();
        $res = $logic->getInnerPayLog($this->map, 'a.*,b.order_sn,c.user_phone,d.driver_name,e.car_plate',$page, $limit);
        $count = Db::table('xm_pay_log a')->join("order_common b",'a.common_id=b.common_id','LEFT')->where($this->map)->count();
        $res = decode_array_2($res);
        echo json_encode(array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $res
        ));
    }

    //搜索
    public function search()
    {
        $page = input('get.page');
        $limit = input('get.limit');
        $start = strtotime(input('get.start'));
        $end = strtotime(input('get.end'));
        $phone = trim(input('get.user_phone'),' ');
        $order_sn = trim(input('get.order_sn'),' ');
        $map = $this->map;
        $phone && $map['c.user_phone'] = Security::encrypt($phone);
        input('get.start')&&$map['a.pay_time'] = array('egt',$start);
        input('get.end')&&$map['a.pay_time'] = array('elt',$end);
        input('get.start')&&input('get.end')&&$map['a.pay_time'] = array('between',array($start,$end));
        $order_sn && $map['b.order_sn'] = array('like',"%$order_sn%");
        $logic = new LogLogic();
        $res = $logic->getInnerPayLog($map, 'a.*,b.order_sn,c.user_phone,d.driver_name,e.car_plate',$page, $limit);
        $count = Db::table('xm_pay_log a')->join("order_common b",'a.common_id=b.common_id','LEFT')
            ->join("user c",'a.uid=c.uid','LEFT')->where($map)->count();
        $res = decode_array_2($res);
        echo json_encode(array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $res
        ));
    }

    //详细
    public function detail()
    {
        $log_id = input('get.id');
        $logic = new LogLogic();
        $info = $logic->getInnerPayLogById($log_id,'a.*,b.order_sn,c.user_phone,d.driver_name,e.car_plate');
        if($info){
            $config['info'] = decode_array($info);
            $config['fields'] = array(
                'order_sn'=>'订单号',
                'user_phone'=>'用户手机号码',
                'driver_name'=>'司机姓名',
                'car_plate'=>'车牌号',
                'order_time'=>'接单时间',
                'order_local'=>'接单经纬度',
                'on_car_time'=>'上车时间',
                'on_car_local'=>'上车经纬度',
                'on_car_name'=>'上车地址',
                'off_car_time'=>'下车时间',
                'off_car_local'=>'下车经纬度',
                'off_car_name'=>'下车地址',
                'pay_time'=>'支付时间',
                'pay_type'=>'支付类型',
                'pay_account'=>'支付账号',
                'order_type'=>'租约车类型',
            );
            $this->assign($config);
            return view();
        }
    }

    //当前位置---转为xmcx服务器使用20180925
    public function local()
    {
        $local = input('get.');
        if($local&&count($local)==5){
            foreach ($local as $key => $val){
                $local[$key] = trim($val);
                if(strpos($key,'local')!==false){
                    $local[$key] = explode(',',trim($val));
                    if(count($local[$key])!=2)unset($local[$key]);
                    $local[$key] = json_encode($local[$key]);
                }
            }
        }
        if($local&&count($local)==5){
            $local['protocol'] = PROTOCOL;
            $this->assign($local);
            return view();
        }
    }

}